package com.wise.site.more;

import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;
import com.wise.site.R;
import com.wise.site.utils.QQWeiboUtil;
import com.wise.site.utils.SinaWeiboUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class WeiboSettingActivity extends Activity implements View.OnClickListener, OnCheckedChangeListener {
	
	private static final int REFRESHQQWEIBO = 10000;
	private static final int REFRESHSINAWEIBO = REFRESHQQWEIBO + 1;
	
	private CheckBox mQQWeiboCheckBox;
	private CheckBox mSinaWeiboCheckBox;
	private TextView mQQWeiboUserName;
	private TextView mSinaWeiboUserName;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_websetting_activity);
		((TextView)findViewById(R.id.activity_title)).setText(getString(R.string.weibo_setting));
		findViewById(R.id.title_back).setOnClickListener(this);
		mQQWeiboCheckBox = (CheckBox) findViewById(R.id.qq_weibo_check);
		mSinaWeiboCheckBox = (CheckBox) findViewById(R.id.sina_weibo_check);
		mQQWeiboUserName = (TextView) findViewById(R.id.qq_weibo_user_name);
		mSinaWeiboUserName = (TextView) findViewById(R.id.sina_weibo_user_name);
		mQQWeiboCheckBox.setOnCheckedChangeListener(this);
		mSinaWeiboCheckBox.setOnCheckedChangeListener(this);
		refreshSinaWeiboData();
	}
	
	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch(id){
		case R.id.title_back:
			finish();
			break;
		}
	}
	
	@Override
	public void onCheckedChanged(CompoundButton button, boolean checked) {
		if(button.getId() == R.id.sina_weibo_check){
			if(checked){
				String sinaUserName = SinaWeiboUtil.getAuthorizeUserName(this);
				if(sinaUserName == null){
					SinaWeiboUtil.callAuthorizeUI(this);
				}
			}else{
				SinaWeiboUtil.logOut(this);
				
			}
		}else if(button.getId() == R.id.qq_weibo_check){
			if(checked){
				String qqUserName = QQWeiboUtil.getAuthorizeUserName(this);
				if(qqUserName == null){
					QQWeiboUtil.callAuthorizeUI(this);
				}
			}else{
				QQWeiboUtil.logOut(this);
				refreshQQWeiboData();
			}
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == QQWeiboUtil.RESULT && resultCode == OAuthV2AuthorizeWebView.RESULT_CODE){
			OAuthV2 oauth = (OAuthV2) data.getExtras().getSerializable("oauth");
			String msg = oauth.getMsg();
			String split[] = oauth.getMsg().split("&");
			String userName = split[split.length - 2].split("=")[1];
			
			if(QQWeiboUtil.authorize(this, userName, oauth.getAccessToken(), oauth.getOpenid(), oauth.getClientId())){
				refreshQQWeiboData();
			}
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		refreshQQWeiboData();
		refreshSinaWeiboData();
	}
	
	public void refreshQQWeiboData(){
		/*String qqUserName = QQWeiboUtil.getAuthorizeUserName(this);
		if(qqUserName == null){
			mQQWeiboUserName.setText(R.string.qq_weibo_text);
			mQQWeiboCheckBox.setChecked(false);
		}else{
			mQQWeiboUserName.setText(qqUserName);
			mQQWeiboCheckBox.setChecked(true);
		}*/
		mHandler.sendEmptyMessage(REFRESHQQWEIBO);
	}
	
	public void refreshSinaWeiboData(){
		/*String sinaUserName = SinaWeiboUtil.getAuthorizeUserName(this);
		if(sinaUserName == null){
			mSinaWeiboUserName.setText(R.string.sina_weibo_text);
			mSinaWeiboCheckBox.setChecked(false);
		}else{
			mSinaWeiboUserName.setText(sinaUserName);
			mSinaWeiboCheckBox.setChecked(true);
		}*/
		mHandler.sendEmptyMessage(REFRESHSINAWEIBO);
	}
	
	Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case REFRESHQQWEIBO:
				String qqUserName = QQWeiboUtil.getAuthorizeUserName(WeiboSettingActivity.this);
				if(qqUserName == null){
					mQQWeiboUserName.setText(R.string.qq_weibo_text);
					mQQWeiboCheckBox.setChecked(false);
				}else{
					mQQWeiboUserName.setText(qqUserName);
					mQQWeiboCheckBox.setChecked(true);
				}
				break;
			case REFRESHSINAWEIBO:
				String sinaUserName = SinaWeiboUtil.getAuthorizeUserName(WeiboSettingActivity.this);
				if(sinaUserName == null){
					mSinaWeiboUserName.setText(R.string.sina_weibo_text);
					mSinaWeiboCheckBox.setChecked(false);
				}else{
					mSinaWeiboUserName.setText(sinaUserName);
					mSinaWeiboCheckBox.setChecked(true);
				}
				break;
			}
		};
	};
	
	
}
